<template>
  <div class="app-container">
    <el-card class="box-card">
      <el-button size="default" type="primary" @click="handleAdd">新增装饰</el-button>

      <div class="flex justify-between">
        <div class="flex justify-end">
          <div class="flex items-center mr-4">
            <span class="item">奖励ID：</span>
            <el-input v-model="listQuery.title" clearable placeholder="请输入奖励ID" size="normal"></el-input>
          </div>
          <div class="flex items-center mr-4">
            <span class="item">奖励名称：</span>
            <el-input v-model="listQuery.title" clearable placeholder="请输入奖励名称" size="normal"></el-input>
          </div>
          <div class="flex items-center mr-4">
            <span>状态：</span>
            <el-select v-model="listQuery.state"
                       class="filter-item marginRB10"
                       filterable
                       placeholder="请选择状态"
                       style="width: 200px"
            >
              <el-option
                v-for="item in selectList2"
                :key="item.key"
                :label="item.lab"
                :value="item.key"
              ></el-option>
            </el-select>

          </div>
          <el-button size="default" type="primary" @click="getTableData">搜索</el-button>
          <el-button size="default" type="primary" @click="handleReset">重置</el-button>
        </div>
      </div>


      <el-table
        ref="multipleTable"
        v-loading="loading"
        :data="tableData"
        border
        class="mt-3"
        stripe
        style="width: 100%"
        tooltip-effect="dark"
      >
        <el-table-column :index="indexMethod" label="序号" prop="user_code" type="index" width="120px"></el-table-column>
        <el-table-column label="装饰类别" prop="title"></el-table-column>

        <el-table-column label="创建时间" prop="created_at"></el-table-column>
        <el-table-column label="操作" width="200px">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              @click="handleEdit(scope.row)"
            >编辑
            </el-button>
            <el-button
              size="mini"
              type="text"
              @click="handleSDelete(scope.row)"
            >删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <cus-pagination
        v-show="total > 0"
        :limit.sync="listQuery.per_page"
        :page.sync="listQuery.page"
        :total="total"
        @pagination="getTableData"
      />
    </el-card>

    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="30%"
      @close="closeDialog"
    >
      <el-form ref="form" :inline="false" :model="formData" :rules="rules" label-width="120px" size="normal">
        <el-form-item label="类别名称" prop="title">
          <el-input
            v-model="formData.title"
            placeholder="请输入类别名称"
          />
        </el-form-item>
        <el-form-item label="值" prop="value">
          <el-input
            v-model="formData.value"
            placeholder="请输入值"
          />
        </el-form-item>
        <el-form-item label="类别图标" prop="icon">
          <el-upload
            ref="upload"
            :auto-upload="true"
            :before-upload="beforeAvatarUpload"
            :file-list="fileList"
            :http-request="customizeUpload"
            :limit="1"
            :on-remove="handlePictureRemove"
            :on-success="handlePictureSuccess"
            action=""
            class="upload-demo"
            list-type="picture"
          >
            <el-button v-if="!formData.icon" slot="trigger" size="small" type="primary">上传展示图片</el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input
            v-model="formData.sort"
            placeholder="请输入排序"
          />
        </el-form-item>
      </el-form>

      <span slot="footer">
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="handleOnSubmit">确定</el-button>
            </span>
    </el-dialog>

  </div>
</template>

<script>
import { decorateType, delDecorateType, postDecorateType, putDecorateType } from '@/api/decorate'

export default {
  name: 'wishingList',
  components: {},
  data() {
    return {
      selectList2: [
        { key: -1, lab: '全部状态' },
        { key: 1, lab: '启用 ' },
        { key: 2, lab: '禁用' }
      ],
      dialogVisible: false,
      dialogVisible2: false,
      formData: {
        title: undefined,
        value: undefined,
        icon: undefined,
        sort: undefined,
        id: undefined
      },
      rules: {
        title: [{ required: true, message: '请输入装饰类别名称', trigger: 'blur' }]
      },
      loading: false,
      tableData: [],
      listQuery: {
        page: 1,
        per_page: 10
      },
      total: 1,
      options: [
        {
          value: 1,
          label: '123'
        }
      ],
      fileList: [],
      dialogTitle: undefined
    }
  },
  created() {
    this.getTableData()
  },
  methods: {
    indexMethod(index) {
      return this.total - (this.listQuery.page - 1) * this.listQuery.per_page - index
    },
    // 网络请求
    /**
     * getTableData
     * @desc 获取表格数据
     */
    async getTableData() {
      this.loading = true
      for (let key in this.listQuery) {
        this.listQuery[key] = !this.listQuery[key] ? undefined : this.listQuery[key]
      }
      const { data, code } = await decorateType(this.listQuery)
      if (code === 200) {

        this.tableData = data.list
        this.total = data.meta.total
      }
      this.loading = false

    },
    closeDialog() {
      if (this.$refs['form']) {
        this.$refs['form'].clearValidate() // 清除整个表单的校验
        this.$refs['form'].resetFields() // 清除整个表单的校验
      }
      this.fileList = []
      this.formData = {
        title: undefined,
        value: undefined,
        icon: undefined,
        sort: undefined,
        id: undefined
      }
    },

    handleOnSubmit() {

      this.$refs['form'].validate(valid => {
        if (valid) {

          for (let key in this.formData) {
            this.formData[key] = !this.formData[key] ? undefined : this.formData[key]
          }
          if (this.formData.id) {
            putDecorateType(this.formData).then(res => {
              if (res.code === 200) {
                this.$message({
                  type: 'success',
                  message: '操作成功'
                })
              }
              this.dialogVisible = false
              this.getTableData()
            })
          } else {
            postDecorateType(this.formData).then(res => {
              if (res.code === 200) {
                this.$message({
                  type: 'success',
                  message: '操作成功'
                })
              }
              this.dialogVisible = false
              this.getTableData()
            })
          }

        }
      })
    },
    handleEdit(row) {
      this.dialogTitle = '编辑'
      this.formData = JSON.parse(JSON.stringify(row))
      this.fileList = [{
        url: row.icon
      }]
      this.dialogVisible = true
    },
    handleSDelete(row) {
      this.$confirm('确定删除该类别？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delDecorateType(row.id).then(res => {
          if (res.code === 200) {
            this.$message({
              type: 'success',
              message: '操作成功'
            })
          }
          this.dialogVisible = false
          this.getTableData()
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    handleReset() {
      this.listQuery = {
        page: 1,
        per_page: 10
      }
      this.getTableData()
    },
    handleAdd() {
      this.dialogTitle = '新增'
      this.dialogVisible = true
    },
    beforeAvatarUpload(file) {
      // const isJPG = file.type === 'image/jpeg' || file.type === 'image/png' || file.type === 'image/webp' || file.type === 'image/gif'
      // const isLt2M = file.size / 1024 / 1024 < 2

      // if (!isJPG) {
      //     this.$message.error('请上传JPG/PNG格式的图片!')
      // }
      // if (!isLt2M) {
      //     this.$message.error('上传图片大小不能超过 2MB!')
      // }
      // return isJPG && isLt2M
    },
    handlePictureSuccess() {
      this.formData.icon = this.ossLastImgUrl
    },
    handlePictureRemove() {
      this.fileList = []
      this.formData.icon = undefined
    }
  }
}
</script>

<style lang="scss" scoped>
.infoBox {
  margin-top      : 15px;
  margin-bottom   : 15px;
  padding-bottom  : 15px;
  border          : 1px solid #999999;
  border-radius   : 10px;
  display         : flex;
  align-items     : baseline;
  justify-content : center;
  box-sizing      : border-box;
  padding-right   : 30px;

  .info_1 {
    display         : flex;
    justify-content : center;
    flex-direction  : column;
    width           : 100%;

    .info_2 {
      margin-top  : 15px;
      display     : flex;
      align-items : center;
      position    : relative;

      span {
        width      : 180px;
        text-align : right;
      }

      .tip {
        color     : red;
        font-size : 12px;
        position  : absolute;
        top       : 100%;
        left      : 120px;
      }
    }
  }
}

.item {
  width : 100px;
}

.formItemBox {
  display     : flex;
  align-items : center;
  flex-wrap   : wrap;

  width       : 100%;
  overflow    : hidden;
}

::v-deep {
  .formItemBox {
    .el-form-item {
      width : 50% !important;
    }
  }

}

</style>

